package gdma.hqdes.webapp.controller;

import common.exception.BadRequestException;
import gdma.hqdes.bean.ArticleTemplate;
import gdma.hqdes.service.ArticleService;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

import static gdma.hqdes.Constants.ROLE_STATISTICIAN;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-08-30
 */
@RestController
@RequestMapping("/api/admin")
@RequiresRoles(ROLE_STATISTICIAN)
public class ArticleAdminController {

    public static final File APP_PATH = new File(".");
    public static final String ARTICLE_TEMPLATE_ROOT = "/files/template";

    @Autowired
    ArticleService articleService;

    @GetMapping("/article-template-vars")
    public String[][] templateVars() {
        return new String[][] {
                { "{{Project.year}}", "年份" },
                { "{{Unit.name}}", "单位名称" },
                { "{{RootIndex.unitScore.score}}", "单位总得分" }
        };
    }

    @GetMapping("/article-templates")
    public Object listTemplates(@RequestParam(value = "keyword", required = false) String keyword,
                                @ApiParam("页码，0开始") @RequestParam(value = "page", defaultValue = "0") int page,
                                @ApiParam("页长，默认20") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        int[] pagination = pageSize != null ? new int[] {page, pageSize} : new int[] {page};
        return articleService.listTemplates(keyword, pagination);
    }

    @PostMapping("/article-templates")
    public Object createTemplate(@RequestBody ArticleTemplate template) {
        File templateFile0 = new File(APP_PATH, template.getFilePath());
        if(!templateFile0.exists() || templateFile0.length() == 0)
            throw new BadRequestException("模板文件不可为空");

        template.setId(null);
        articleService.saveTemplate(template);

        String filePath = ARTICLE_TEMPLATE_ROOT + "/" + template.getId() + ".docx";
        File templateFile = new File(APP_PATH, filePath);
        templateFile.delete();
        templateFile0.renameTo(templateFile);
        template.setFilePath(filePath);
        articleService.saveTemplate(template);

        return template;
    }

    @PostMapping("/article-templates/{id}")
    public Object updateTemplate(@PathVariable("id") Integer id,
                                 @RequestBody ArticleTemplate template) {
        File templateFile0 = new File(APP_PATH, template.getFilePath());
        if(!templateFile0.exists() || templateFile0.length() == 0)
            throw new BadRequestException("模板文件不可为空");

        String filePath = ARTICLE_TEMPLATE_ROOT + "/" + template.getId() + ".docx";
        if(!template.getFilePath().equals(filePath)) {
            File templateFile = new File(APP_PATH, filePath);
            templateFile.delete();
            templateFile0.renameTo(templateFile);
            template.setFilePath(filePath);
        }

        template.setId(id);
        return articleService.saveTemplate(template);
    }

}
